package com.medusa.aps.business.modules.security.mp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.security.model.input.RoleMenuQueryPara;
import com.medusa.aps.business.modules.security.mp.entity.RoleMenu;
import com.medusa.aps.business.modules.security.mp.mapper.RoleMenuMapper;
import com.medusa.aps.business.modules.security.mp.service.IRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: xianhui.yu
 **/
@Service
@Transactional
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    @Autowired
    RoleMenuMapper roleMenuMapper;

    @Override
    public void listPage(Page<RoleMenu> page, RoleMenuQueryPara filter) {
        page.setRecords(roleMenuMapper.selectRoleMenus(page, filter));
    }

    @Override
    public List<RoleMenu> list(RoleMenuQueryPara filter) {
        return roleMenuMapper.selectRoleMenus(filter);
    }

    @Override
    public void saveRoleMenu(RoleMenuQueryPara para) {
        Integer roleId = para.getRoleId();
        String menuIds = para.getMenuIds();
        roleMenuMapper.deleteByRoleId(roleId);
        if (StringUtils.isNotBlank(menuIds)) {
            String[] menuIdArrays = menuIds.split(",");
            for (String menuId : menuIdArrays) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(Long.parseLong(menuId));
                roleMenuMapper.insert(roleMenu);
            }
        }
    }

    @Override
    public Integer insert(RoleMenu para) {
        if (para.getId() != null) {
            roleMenuMapper.updateById(para);
        } else {
            roleMenuMapper.insert(para);
        }
        return para.getId();
    }
}
